package com.py.payment.controller;

import com.py.payment.dto.param.IngredientsRecordParamDto;
import com.py.payment.dto.ret.IngredientsRecordRetDto;
import com.py.payment.entity.PyIngredients;
import com.py.payment.entity.PyIngredientsRecord;
import com.py.payment.service.PyIngredientsRecordService;
import com.py.payment.service.PyIngredientsService;
import com.py.payment.tools.ToolsUtils;
import com.py.payment.tools.date.DateUtils;
import com.py.payment.tools.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/ingredientsRecord")
public class IngredientsRecordController {

    @Autowired
    private PyIngredientsRecordService ingredientsRecordService;

    @Autowired
    private PyIngredientsService ingredientsService;

    @PostMapping(name = "原料投放记录",path = "/getIngredientsRecordList")
    public ResponseData getIngredientsRecordList(@RequestBody IngredientsRecordParamDto ingredientsRecordParamDto) {
        if(ToolsUtils.isNotEmpty(ingredientsRecordParamDto.getEndTime())){
            Date endDate = DateUtils.datestrToDate(ingredientsRecordParamDto.getEndTime(), DateUtils.YYYY_MM_DD_HH_MM_SS);
            String endStr = DateUtils.dateToDateStr(endDate, DateUtils.YYYY_MM_DD);
            endDate = DateUtils.datestrToDate(endStr+" 23:59:59", DateUtils.YYYY_MM_DD_HH_MM_SS);
            ingredientsRecordParamDto.setEndTime(DateUtils.dateToDateStr(endDate,DateUtils.YYYY_MM_DD_HH_MM_SS));
        }
        if(ToolsUtils.isNotEmpty(ingredientsRecordParamDto.getStartTime())){
            Date startDate = DateUtils.datestrToDate(ingredientsRecordParamDto.getStartTime(),DateUtils.YYYY_MM_DD_HH_MM_SS);
            String startStr = DateUtils.dateToDateStr(startDate, DateUtils.YYYY_MM_DD);
            startDate = DateUtils.datestrToDate(startStr+" 00:00:00", DateUtils.YYYY_MM_DD_HH_MM_SS);
            ingredientsRecordParamDto.setStartTime(DateUtils.dateToDateStr(startDate,DateUtils.YYYY_MM_DD_HH_MM_SS));
        }
        List<IngredientsRecordRetDto> ingredientsRecordList = this.ingredientsRecordService.getIngredientsRecordList(ingredientsRecordParamDto);
        return ResponseData.success(ingredientsRecordList);
    }

    @PostMapping(name = "新增记录",path = "/saveIngredientsRecord")
    public ResponseData saveIngredientsRecord(@RequestBody List<IngredientsRecordParamDto> recordList) {
        List<PyIngredients> ingredientsList = this.ingredientsService.list();
        Map<String,PyIngredients> map = new HashMap<>();
        ingredientsList.forEach(item->{
            map.put(item.getTitle(),item);
        });
        List<PyIngredientsRecord> ingredientsRecordList = new ArrayList<>();
        recordList.forEach(item->{
            PyIngredientsRecord ingredientsRecord = new PyIngredientsRecord();
            ingredientsRecord.setSiteId(item.getSiteId());
            ingredientsRecord.setBatchId(item.getBatchId());
            ingredientsRecord.setBillId(item.getBillId());
            ingredientsRecord.setCreateTime(item.getCreateTime());
            ingredientsRecord.setNumericValue(item.getNumericValue());
            ingredientsRecord.setPredefinedNumber(item.getPredefinedNumber());
            ingredientsRecord.setInvestmentId(map.get(item.getInvestmentName()).getId());
            ingredientsRecordList.add(ingredientsRecord);
        });
        this.ingredientsRecordService.saveIngredientsRecord(ingredientsRecordList);
        return ResponseData.success();
    }
}
